﻿@{
    var Productlist = new ProductBLL().GetForSelect(new SelectListItem() { Text = "--全部产品--", Value = "-1" });
    if (Request.Form["ProductID"] != null)
    {
        Productlist.First(i => i.Value == Request.Form["ProductID"]).Selected = true;
    }
    var Statelist = typeof(BuyOrderState).EnumToSelectItems(new SelectListItem() { Text = "--全部状态--", Value = "-1" });
    if (Request.Form["State"] != null)
    {
        Statelist.First(i => i.Value == Request.Form["State"]).Selected = true;
    }
    var Customerlist = new CustomerBLL().GetForSelect(CustomerType.供货商, new SelectListItem() { Text = "--全部供货商--", Value = "-1" });
    if (Request.Form["Customer"] != null)
    {
        Customerlist.First(i => i.Value == Request.Form["Customer"]).Selected = true;
    }
}
<style type="text/css">
    .myicon
    {
        display: block;
        width: 50px;
        height: 20px;
        position: absolute;
        left: 0px;
        bottom: 0px;
    }
</style>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="widget-box">
                <form action="@Url.Action("List", "BuyOrder")" method="post">
                <div class="widget-title">
                    <span class="icon"><i class="icon-th"></i></span>
                    <h5>
                        进货单列表</h5>
                </div>
                <div class="widget-content" style="height: 25px;">
                    <a href="@Url.Action("ParentInfo", "BuyOrder")" class="btn btn-success fl">添 加</a>
                    <input type="button" value="批量删除" class="btn btn-danger fl" onclick="Deletes()" />
                    <div class="span3">
                        @Html.DropDownList("ProductID", Productlist)
                    </div>
                    <div class="span2">
                        @Html.DropDownList("Customer", Customerlist)
                    </div>
                    <div class="span2">
                        @Html.DropDownList("State", Statelist)
                    </div>
                    <div class="span2">
                        @Html.TextBox("OrderDate", Request.Form["OrderDate"], new Dictionary<string, object>() { { "class", "datepicker span2" }, { "data-date-format", "yyyy-mm-dd" }, { "style", "width:95px" }, { "placeholder", "订货日期" } })
                    </div>
                    <button type="submit" class="btn btn-info fl" onclick="return Search();">
                        搜 索</button>
                </div>
                <div class="widget-content">
                    <table class="table table-bordered table-striped with-check">
                        <thead>
                            <tr class="tr-parent">
                                <th style="width: 30px">
                                    <input type="checkbox" id="title-table-checkbox" name="title-table-checkbox" />
                                </th>
                                <th style="width: 100px">
                                    供货商
                                </th>
                                <th style="width: 70px">
                                    订货时间
                                </th>
                                <th style="width: 30px">
                                    汇率
                                </th>
                                <th>
                                    产品总价
                                </th>
                                <th>
                                    运费总价
                                </th>
                                <th>
                                    其他总价
                                </th>
                                <th>
                                    总价
                                </th>
                                <th style="width: 70px">
                                    付款状态
                                </th>
                                <th style="width: 70px;">
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (BuyParentList item in ViewBag.PagList.DataList)
                            {
                                List<BuyOrderList> Lists = ViewBag.OrderList[item.ID] ?? new List<BuyOrderList>();
                                decimal Price = Lists.Sum(i => (i.Price ?? 0) * (i.Num ?? 0));
                                decimal ExpressPrice = Lists.Sum(i => i.ExpressPrice ?? 0);
                                decimal OtherPrice = Lists.Sum(i => i.OtherPrice ?? 0);
                                decimal ALLPrice = Lists.Sum(i => i.AllPrice ?? 0);
                                <tr>
                                    <td style="position: relative;">
                                        <input type="checkbox" name="ID" value="@item.ID" title="@item.CreateTime" />
                                        <a href="javascript:void(0);" onclick="ShowSon(this)" class="myicon" title="显示所有子单">
                                            <i class="icon-caret-down"></i></a>
                                    </td>
                                    <td>
                                        @item.CustomerName
                                    </td>
                                    <td>
                                        @item.OrderTime.ToShortStr()
                                    </td>
                                    <td>
                                        @item.KoreaRate
                                    </td>
                                    <td>
                                        <font color="red">@Price.ToString("0.00") 元</font>
                                    </td>
                                    <td>
                                        <font color="red">@ExpressPrice.ToString("0.00") 元</font>
                                    </td>
                                    <td>
                                        <font color="red">@OtherPrice.ToString("0.00") 元</font>
                                    </td>
                                    <td>
                                        <font color="red">@ALLPrice.ToString("0.00") 元</font>
                                    </td>
                                    <td>
                                        <input type="button" onclick="SetPayState(@item.ID,@item.State)" value="@item.StateName" class="btn @(item.State == 1 ? "btn-danger" : "btn-success") btn-mini" />
                                    </td>
                                    <td style="line-height: 26px;">
                                        <a class="btn btn-success btn-mini" href="@Url.Action("Info", "BuyOrder", new { ParentID = item.ID })">
                                            +子单</a>
                                        <!---->
                                        <a class="btn btn-primary btn-mini" href="@Url.Action("ParentInfo", "BuyOrder", new { id = item.ID })">
                                            编 辑&nbsp; </a>
                                        <!---->
                                        <input type="button" onclick="Delete(@item.ID)" value=" 删 除 " class="btn btn-danger btn-mini" />
                                    </td>
                                </tr>
                                <tr style="background-color: #eeeeee; display: none">
                                    <td style="border-right: 0px;">
                                        所有子单
                                    </td>
                                    <td colspan="9" style="border-left: 0px; padding-left: 0px">
                                        <table class="table table-bordered" style="margin: 0px;">
                                            <thead>
                                                <tr>
                                                    <th style="width: 80px">
                                                        产品
                                                    </th>
                                                    <th>
                                                        数量
                                                    </th>
                                                    <th>
                                                        折扣
                                                    </th>
                                                    <th>
                                                        单价(&yen;/ ₩)
                                                    </th>
                                                    <th>
                                                        运费
                                                    </th>
                                                    <th>
                                                        其他
                                                    </th>
                                                    <th>
                                                        总价/单个总价
                                                    </th>
                                                    <th style="width: 70px;">
                                                        出货状态
                                                    </th>
                                                    <th style="width: 95px;">
                                                        操作
                                                    </th>
                                                </tr>
                                            </thead>
                                            @foreach (BuyOrderList Info in Lists)
                                            {
                                                <tr>
                                                    <td>
                                                        <a target="_blank" title="@Info.Createtime" href="@Url.Action("Info", "Product", new { id = @Info.ProductID })">@Info.ProductName</a>
                                                    </td>
                                                    <td>
                                                        @Info.Num
                                                    </td>
                                                    <td>
                                                        @Info.Discount
                                                    </td>
                                                    <td>
                                                        <font color="#3366CC">@((Info.Price ?? 0).ToString("0.00"))元 </font>/ <font color="red">
                                                            @(Math.Round((Info.Price ?? 0) * (item.KoreaRate ?? 0)))韩元</font>
                                                    </td>
                                                    <td>
                                                        <font color="#3366CC">@((Info.ExpressPrice ?? 0).ToString("0.00"))
                                                            元</font>
                                                    </td>
                                                    <td>
                                                        <font color="#3366CC">@((Info.OtherPrice ?? 0).ToString("0.00"))
                                                            元</font>
                                                    </td>
                                                    <td>
                                                        <font color="#3366CC">@((Info.AllPrice ?? 0).ToString("0.00"))元</font>/<font color="red">@(((Info.AllPrice ?? 0) / (Info.Num ?? 1)).ToString("0.00"))元</font>
                                                    </td>
                                                    <td>
                                                        @{
                                                                                                                  string stateClass = "";
                                                                                                                  switch (((BuyOrderState)(Info.State)))
                                                                                                                  {
                                                                                                                      case BuyOrderState.待出货:
                                                                                                                          stateClass = "btn-inverse";
                                                                                                                          break;
                                                                                                                      case BuyOrderState.已出货:
                                                                                                                          stateClass = "btn-success";
                                                                                                                          break;
                                                                                                                      case BuyOrderState.发货中:
                                                                                                                          stateClass = "btn-warning";
                                                                                                                          break;
                                                                                                                      case BuyOrderState.已收货:
                                                                                                                          stateClass = "";
                                                                                                                          break;
                                                                                                                  }
                                                            <input data-content="出货时间：@Info.ShipTime.ToShortStr()<br/>发货时间：@Info.SendTime.ToShortStr()<br/>收货时间：@Info.ReceivedTime.ToShortStr()" data-placement="top" data-toggle="popover" data-original-title="货运具体时间" class="btn btn-mini popoverbtn @stateClass" onclick="SetState(@Info.ID,@Info.State)"  type="button" value="@Info.StateName"  />
                                                        }
                                                    </td>
                                                    <td style="line-height: 30px;">
                                                        <a class="btn btn-primary btn-mini" href="@Url.Action("Info", "BuyOrder", new { id = Info.ID, ParentID = Info.ParentID })">
                                                            编 辑&nbsp;</a>
                                                        <!---->
                                                        <input  type="button" value=" 删 除 " class="btn btn-danger btn-mini" onclick="DeleteSon(@Info.ID)" />
                                                    </td>
                                                </tr>
                                            }
                                        </table>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                    @Html.Partial("_Paging")
                </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    //显示子单
    function ShowSon(obj) {
        if ($(obj).children("i").hasClass("icon-caret-down")) {
            $(obj).children("i").removeClass("icon-caret-down").addClass("icon-caret-up");
            $(obj).parents("tr").next().show();
        } else {
            $(obj).children("i").removeClass("icon-caret-up").addClass("icon-caret-down");
            $(obj).parents("tr").next().hide();
        }
    }
    //删除父单
    function Delete(id) {
        if (confirm("确认删除？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("Delete", "BuyOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                if (d == "False") {
                    alert("子单中包含已收货的订单无法删除！");
                }
                else {
                    $("form").submit();
                }

            })
        }
    }
    //删除子单
    function DeleteSon(id) {
        if (confirm("确认删除子单？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("DeleteSon", "BuyOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                if (d == "False") {
                    alert("已收货的订单无法删除！");
                }
                else {
                    $("form").submit();
                }
            })
        }
    }
    //批量删除
    function Deletes() {
        var obj = $("input:checkbox[name='ID']:checked");
        if (obj.length == 0) { alert("请至少选择一条数据！"); return; }
        var ids = ""; obj.each(function () { ids += $(this).val() + ","; });
        if (confirm("确认删除" + obj.length + "条数据？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("Delete", "BuyOrder")', "id=" + ids, function (d) {
                $("#LoadingBox").hide();
                //刷新页面，页码变为第一页
                $('#PageIndex').val(1);
                $("form").submit();
            })
        }
    }
    //设置付款状态
    function SetPayState(id, state) {
        if (state == 1 && confirm("确定修改状态为已付款？")) {
            $("#LoadingBox").show();
            $.post('@Url.Action("SetPayState", "BuyOrder")', "id=" + id, function (d) {
                $("#LoadingBox").hide();
                $("form").submit();
            })
        }
    }
    //设置出货状态
    function SetState(id, state) {
        var statename;
        switch (state) {
            case 1:
                statename = "已出货";
                break;
            case 2:
                statename = "发货中";
                break;
            case 3:
                statename = "已收货";
                break;
        }
        if (state != 4) {
            if (confirm("确定修改状态为" + statename + "？")) {
                $("#LoadingBox").show();
                $.post('@Url.Action("SetState", "BuyOrder")', "id=" + id, function (d) {
                    $("#LoadingBox").hide();
                    $("form").submit();
                })
            }
        }
    }
</script>
